package com.hq.fxh.No5_User.User.Fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Intefers.NetworkSubscriber;
import com.hq.fxh.Modle.GetEntertainmentListRes;
import com.hq.fxh.Modle.GetEntertainmentListResInfo;
import com.hq.fxh.Modle.GetMyPostModle;
import com.hq.fxh.Modle.GetVideoListModle;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.item.ToastUtils;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Model.CommonRes;
import com.hq.fxh.No5_User.User.Adapter.Ladapter_GZ_Plate;
import com.hq.fxh.No5_User.User.Adapter.Ladapter_MyPlate;
import com.hq.fxh.No5_User.User.Adapter.Ladapter_MyVideo;
import com.hq.fxh.R;
import com.hq.fxh.Utils.AppConstant;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.SharedPreferenceUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import rx.Subscriber;

import static android.R.attr.data;
import static cn.jpush.android.api.JPushInterface.a.d;
import static cn.jpush.android.api.JPushInterface.a.e;
import static cn.jpush.android.api.JPushInterface.a.p;
import static com.hq.fxh.R.id.recyclerView;
import static com.hq.fxh.R.id.refresh;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/9/15
 * @Email xlwenhao1858@sina.com
 */


public class Fragment_MyPlate_Video extends Fragment {
    protected View rootView;
    int page = 1;
    protected PullToRefreshListView listGZPList;
    private Ladapter_MyVideo adapter;
    List<GetVideoListModle.DataBean>  videoData;
    private int memberId;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_gz_people, null);

        initView(rootView);

        GetVideo( );
        return rootView;
    }

    private void initView(View rootView) {
        memberId = SharedPreferenceUtils.getMemberId(getContext());
        listGZPList = (PullToRefreshListView) rootView.findViewById(R.id.list_GZP_List);
        initData();
    }

    private void initData() {

        listGZPList.setMode(PullToRefreshBase.Mode.BOTH);
        listGZPList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                GetVideo();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                ++page;
                GetVideo();
            }
        });
        adapter = new Ladapter_MyVideo();
        adapter.setOnDeleteCallBack(new Ladapter_MyVideo.OnDeleteCallBack() {
            @Override
            public void deleteCall(int position, GetEntertainmentListResInfo data) {
                LogUtils.e("删除成功");
                if (data == null) {
                    LogUtils.startToash(getContext(), "视频删除失败");
                    return;
                }
                String subject_id = data.getSubject_id();
                showDialog(data.getSubject_title(), subject_id, position);
            }
        });
        listGZPList.setAdapter(adapter);

    }

    public static Fragment_MyPlate_Video newInstance() {

        Bundle args = new Bundle();

        Fragment_MyPlate_Video fragment = new Fragment_MyPlate_Video();
        fragment.setArguments(args);
        return fragment;
    }


    public void GetVideo( ){

        UserServiceImpl.getVideo(AppConstant.卫平id, 1, 20, new Subscriber<GetVideoListModle>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(GetVideoListModle responseBody) {
                try {
                    LogUtils.e( "新接口"+ responseBody.toString());
                    List<GetVideoListModle.DataBean> data = responseBody.getData();
                    if(data==null||data.size()==0){

                    }else {

                    }
                    if (data != null && data.size() != 0) {
                        if (videoData == null) {
                            videoData = new ArrayList<>();
                        }
                        if (page == 1) {
                            videoData.clear();
                        }
                        videoData.addAll(data);
                        adapter.setData(videoData);
                        adapter.notifyDataSetChanged();

                    } else {
                        LogUtils.startToash(getContext(), AppConstant.NullData);
                    }
                } catch ( Exception e) {


                }

            }
        });

    }



    public void delete(String videoId, int position) {
        UserServiceImpl.delMyVideo(videoId, 1, new Subscriber<CommonRes>() {
            @Override
            public void onCompleted() {
                LogUtils.e("删除com");

            }

            @Override
            public void onError(Throwable e) {
                LogUtils.e("删除error");

            }

            @Override
            public void onNext(CommonRes commonRes) {
                LogUtils.e("删除next");

            }
        });
    }

    private void showDialog(String title, final String posid, final int position) {
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        if (title.length() > 5) {
            title = title.substring(0, 4);
        }

        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(getContext());
        normalDialog.setIcon(R.mipmap.ldd_logo);
        normalDialog.setTitle("提示");
        normalDialog.setMessage("你确定要删除帖子‘" + title + "’?");
        normalDialog.setPositiveButton("删除",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        delete(posid, position);
                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
        // 显示
        normalDialog.show();
    }

}
